package com.market.controller.wx;

import com.market.bean.MarketCoupon;
import com.market.bean.MarketUser;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.BaseRespVo;
import com.market.bean.common.CommonData;
import com.market.bean.vo.wx.WxCouponMyListVo;
import com.market.service.wx.WxCouponService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author:WalterChan
 * @Decription:WxCouponController
 * @Date Created in 2023-01-02-15:56
 */
@RestController
@RequestMapping(("wx/coupon"))
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;

    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info){
        CommonData<MarketCoupon> data =wxCouponService.list(info);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("mylist")
    public BaseRespVo myList(BasePageInfo info,Integer status){
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId=primaryPrincipal.getId();
        //        默认userid=4，等待整合subject
//        Integer userId=4;
        CommonData<WxCouponMyListVo>data = wxCouponService.myList(info,status,userId);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("selectlist")
    public BaseRespVo selectList(Integer cartId,Integer grouponRulesId){
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
//        默认userid=4，等待整合subject
        Integer userId=primaryPrincipal.getId();

        CommonData<WxCouponMyListVo>data =wxCouponService.selectlist(cartId,userId);
        return BaseRespVo.ok(data);
    }
    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody Map<String,Integer>map){
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId=primaryPrincipal.getId();
        //        默认userid=4，等待整合subject
//        Integer userId=4;
        Integer couponId = map.get("couponId");

        Integer result =wxCouponService.receive(couponId,userId);

        if (result==740){
        BaseRespVo baseRespVo = BaseRespVo.invalidData("优惠券已经领取过");
        baseRespVo.setErrno(740);
        return baseRespVo;
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map<String,String> map){
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId=primaryPrincipal.getId();
        //        默认userid=4，等待整合subject
//        Integer userId=4;
        String code = map.get("code");
        BaseRespVo baseRespVo =wxCouponService.exchange(userId,code);
        return baseRespVo;

    }



}
